
public class P34 {
	public static void main(String[] args) {
		int[] factorial = new int[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
				
		for (int i = 1; i < 10; i++) {
			for (int j = 1; j <= i; j++) {
				factorial[i] *= j;  
			}			
		}
		
		for (int i = 0; i < 10; i++) {
			System.out.println(factorial[i]);
		}
		System.out.println();
		
		
		int n = 10;
		int sum = 0;
		
		while (true) {
			String num = String.valueOf(n);
			int max = num.length() * factorial[9];
			
			if (n > max) {
				System.out.println(n);
				break;
			}
			
			int factorialSum = 0;
			//System.out.println(num);
			for (int i = 0; i < num.length(); i++) {				
				//System.out.println(num.substring(i, i + 1));
				factorialSum += factorial[Integer.parseInt(num.substring(i, i + 1), 10)];
			}
			
			if (factorialSum == n) {
				System.out.println(n);
				sum += n;
			}
			n++;
		}
		
		System.out.println();
		System.out.println(sum);
	}
}
